/*
 * 
 * 
 * 
 */
package com.hboxs.asl.service.impl;

import com.hboxs.asl.dao.QuestionTradeDao;
import com.hboxs.asl.entity.Member;
import com.hboxs.asl.entity.Question;
import com.hboxs.asl.entity.QuestionTrade;
import com.hboxs.asl.entity.Sn;
import com.hboxs.asl.service.QuestionTradeService;
import com.hboxs.asl.service.SnService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Service - 易问易答流水
 */
@Service("questionTradeServiceImpl")
public class QuestionTradeServiceImpl extends BaseServiceImpl<QuestionTrade, Long> implements QuestionTradeService {

    @Resource(name = "questionTradeDaoImpl")
    private QuestionTradeDao questionTradeDao;
    @Resource(name = "snServiceImpl")
    private SnService snService;

    @Resource(name = "questionTradeDaoImpl")
    public void setBaseDao(QuestionTradeDao questionTradeDao) {
        super.setBaseDao(questionTradeDao);
    }


    @Override
    public QuestionTrade findBySn(String sn) {
        return questionTradeDao.findBySn(sn);
    }

    /**
     * 判断用户是否购买
     * @param member
     * @param questionId
     * @param paymentStatus
     * @return
     */
    @Override
    public boolean isPay(Member member, Long questionId, QuestionTrade.PaymentStatus paymentStatus) {
        return questionTradeDao.isPay(member, questionId, paymentStatus);
    }

    /**
     * 生成流水
     * @param question
     * @return
     */
    @Override
    public String generateTrade(Question question) {
        QuestionTrade questionTrade = new QuestionTrade();

        BeanUtils.copyProperties(questionTrade, question, "member", "teacher");
        if (question.getMember() != null) {
            questionTrade.setBuyer(question.getMember());
            questionTrade.setMemberName(question.getMember().getName());
            questionTrade.setMemberId(question.getMember().getId());
        }
        if (question.getTeacher() != null) {
            questionTrade.setTeacherName(question.getTeacher().getUsername());
            questionTrade.setTeacherId(question.getTeacher().getId());
        }
        questionTrade.setSn(snService.generate(Sn.Type.defray));
        this.save(questionTrade);
        return questionTrade.getSn();
    }
}
